using System;
using System.Collections;
using System.Collections.Generic;
using BitMango.Diagnostics;
using Tasks;
using UnityEngine;

internal class MonoTask : MonoBehaviour
{
	public bool paused { private get; set; }

	private void Awake()
	{
		this._enumerators = new List<IEnumerator>();
		this.paused = false;
	}

	public void StartCoroutineManaged(IEnumerator task)
	{
		this._enumerators.Add(new SingleTask(task).GetEnumerator());
	}

	public void ExecuteOnMainThread(Action action)
	{
		this.executeOnMainThreadQueue.Enqueue(action);
	}

	private void FixedUpdate()
	{
		if (!this.paused)
		{
			this._enumerators.RemoveAll((IEnumerator enumerator) => !enumerator.MoveNext());
		}
	}

	private void Update()
	{
		while (this.executeOnMainThreadQueue.Count > 0)
		{
			Action action = this.executeOnMainThreadQueue.Dequeue();
			if (action != null)
			{
				action();
			}
			else
			{
				Debugger.LogWarning("MonoTask.Update executeOnMainThreadQueue action is null");
			}
		}
	}

	private List<IEnumerator> _enumerators;

	private Queue<Action> executeOnMainThreadQueue = new Queue<Action>();
}
